package com.wn.consumer.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.wn.bean.Order;
import com.wn.consumer.feign.OrderFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author Yellow Docker
 * @since 2022-11-04
 */
@Api(tags = "orderconsumer模型的接口")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderFeignClient orderFeignClient;

    @ApiOperation("查询所有")
    @GetMapping("all")
    public List<Order> findOrders(){
        List<Order> list = orderFeignClient.findOrders();
        //使用流进行操作
        List<Order> orders = list.stream().sorted((o1, o2) -> o1.getTotal() - o2.getTotal())
                .collect(Collectors.toList());

        return orders;
    }



    @ApiOperation("添加一个order")
    @PostMapping
    public String findOrders(@RequestBody Order order){
        String result = orderFeignClient.addOrder(order);
        return result;
    }

    /**
     *  热点数据的资源的限流
     *  @SentinelResource  热点资源
     *
     *  fallback="exceptionHandler"   失败执行的方法
     *
     *  如果 你的热点资源的返回的结果过 和   fallback 和返回结果要一致
     *
     * @param id
     * @return
     */
    @ApiOperation("通过id查询")
    @GetMapping("{id}")
    @SentinelResource(value="findOrderById",fallback="exceptionHandler")
    public Order findOrderById(@PathVariable("id")Integer id){
        Order order = orderFeignClient.findOrderById(id);
        return order;
    }

    public Order exceptionHandler(Integer id){
        Order order = new Order();
        order.setName("限流的order");
        order.setPrice(20);
        return order;
    }

}

